package com.jeomo.masterdata.starter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.masterdata.starter.entity.Brand;
import com.jeomo.masterdata.starter.entity.Merchant;
import com.jeomo.masterdata.starter.mapper.BrandMapper;
import com.jeomo.masterdata.starter.mapper.MerchantMapper;
import com.jeomo.masterdata.starter.service.IBrandService;
import com.jeomo.masterdata.starter.util.CommonUtil;
import com.jeomo.shiro.dto.LoginInfo;
import com.jeomo.shiro.util.LoginSessionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h3>jeomo</h3>
 * <p>品牌维护</p>
 *
 * @author : 崔贺
 * @date : 2020-08-28 14:46
 **/
@Service
public class BrandServiceImpl extends BaseServiceImpl<BrandMapper, Brand> implements IBrandService {

    @Resource
    private BrandMapper brandMapper;
    @Resource
    private MerchantMapper merchantMapper;

    @Override
    public int create(Brand brand) {
        LoginInfo currentLoginInfo = LoginSessionUtil.getCurrentLoginInfo();
        brand.setOrgCode(currentLoginInfo.getCurrentOrgCode());
        brand.setTopOrgCode(currentLoginInfo.getTopOrgCode());
        String recentlyCode = brandMapper.queryRecentlyCode(brand.getOrgCode());
        String code = CommonUtil.spliceCode(brand.getOrgCode(), recentlyCode);
        brand.setCode(code);
        return brandMapper.insert(brand);
    }

    @Override
    public Boolean removeBrand(String brandCode) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_code", brandCode);
        List<Merchant> merchants = merchantMapper.selectList(queryWrapper);
        if (merchants.isEmpty()) {
            brandMapper.deleteById(brandCode);
            return true;
        } else {
            return false;
        }
    }

//    @Override
//    public PageResponseDto pageSearch(PageQueryDto dto) {
//        //1. 处理查询参数
//        Map<String, Object> searchParams = dto.getSearchParams();
//        searchParams.put(SearchParam.SEARCH_EQ + "org_code", LoginSessionUtil.getCurrentLoginInfo().getCurrentOrgCode());
//        return super.pageSearch(dto);
//    }
}
